﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DBUtility;
using System.IO;
using System.Configuration;

namespace MultiTools.DBTools
{
    public partial class FMDB2Class : Form
    {
        List<string> content;
        string module = "        public string T { get; set; }";
        public FMDB2Class()
        {
            InitializeComponent();

        }

        private void fmDB2ClassTools_Load(object sender, EventArgs e)
        {
            cbConn.SelectedIndex = 1;   
        }

        private void tbSubmit_Click(object sender, EventArgs e)
        {

            ConnectionStringSettings s=ConfigurationManager.ConnectionStrings[cbConn.Text];
            content = new List<string>();
            content.Add("using System;");
            content.Add("using System.Collections.Generic;");
            content.Add("using System.Text;");
            content.Add("");
            if (!string.IsNullOrEmpty(tbNameSpace.Text))
                content.Add("namespace " + tbNameSpace.Text);
            else
                content.Add("namespace SpaceNameHere");

            content.Add("{");
            content.Add("    [Serializable]");

            if (!string.IsNullOrEmpty(tbClassName.Text))
                content.Add("    public class " + tbClassName.Text);
            else
                content.Add("    public class ClassNameHere");

            content.Add("    {");
            ////填写数据
            string sql = string.Empty;
            if (!string.IsNullOrEmpty(tbTableName.Text))
                sql = @"select * from " + tbTableName.Text.ToString() + " where rownum<2";
            else
            {
                MessageBox.Show("请填写表名称");
                return;
            }
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sql;
            cmd.ParaList = new List<SqlParameter>();
            //DataTable dt = SOPC_DBUtility.DBHelper.Execute2DataTable(cbConn.SelectedItem.ToString(),sql, paras.ToArray());
            DataTable dt = DBHelper.Execute2DataTable(cbConn.Text, cmd);
            if (dt == null)
            {
                MessageBox.Show(" 数据库未返回数据");
                return;
            }
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string tmp = module.Replace("T", dt.Columns[i].ColumnName.ToString().ToLower());
                content.Add(tmp);
            }
            ////end
            content.Add("    }");
            content.Add("}");
            if (WriteFile(content))
                MessageBox.Show("成功");
            content.Clear();
        }

        private bool WriteFile(List<string> content)
        {
            //StreamWriter sw;
            string name;
             if (!string.IsNullOrEmpty(tbClassName.Text))
                   name=tbClassName.Text + ".cs";
                else
                   name="ClassNameHere.cs";
            

            System.Windows.Forms.SaveFileDialog saveDia = new SaveFileDialog(); 
            saveDia.Filter = "代码文件|*.cs"; 
            saveDia.Title = "导出为Excel文件";
            saveDia.FileName=name;
            if (saveDia.ShowDialog() == System.Windows.Forms.DialogResult.OK && !string.Empty.Equals(saveDia.FileName))
            {
                StreamWriter sw = new StreamWriter(saveDia.FileName.ToString(), true);
                foreach (string s in content)
                {
                    sw.WriteLine(s);
                }
                sw.Close();
            }   
            return true;
        }
    }
}
